package com.cardosystems.infra.pages;

import java.util.List;

import jsystem.extensions.report.html.Report;
import jsystem.framework.analyzer.AnalyzerException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.cardosystems.infra.enums.ServerType;

public class UserRegistrationPage extends AbstractPage {

	@FindBy(xpath = "//*[@id=\"email\"]")
	WebElement email;

	@FindBy(id = "emailconf")
	WebElement emailconf;

	@FindBy(id = "first_name")
	WebElement firstName;

	@FindBy(id = "last_name")
	WebElement lastName;

	@FindBy(id = "password")
	WebElement password;

	@FindBy(id = "passconf")
	WebElement passconf;

	@FindBy(id = "submit")
	WebElement submitBtn;

	public UserRegistrationPage(WebDriver driver, ServerType server) {
		super(driver);
		driver.navigate()
				.to("http://" + server
						+ ".cardosystems.com/support/cardo-community?orc=nhmjk");
		driver.switchTo().frame("myframe");
		PageFactory.initElements(driver, this);
	}

	public UserRegistrationPage(WebDriver driver) {
		super(driver);
		driver.switchTo().frame("myframe");
		PageFactory.initElements(driver, this);
	}

	public ProductRegistrationPage registerNewUser(String email,
			String emailConf, String firstName, String lastName,
			String password, String passwordConf) throws Exception {
		fillNewUserDetails(email, emailConf, firstName, lastName, password,
				passwordConf);
		return new ProductRegistrationPage(driver);

	}

	public boolean reigsterNewUserWithWrongDetails(String email,
			String emailConf, String firstName, String lastName,
			String password, String passwordConf) throws Exception {
		fillNewUserDetails(email, emailConf, firstName, lastName, password,
				passwordConf);
		try {
			List<WebElement> errors = driver.findElements(By
					.className("floatErrorInner"));
			// for (WebElement error : errors){
			// WebElement errorList = error.findElement(By.className("errors"));
			// List<WebElement> li = errorList.findElements(By.tagName("li"));
			// List<WebElement> allElements =
			// for (WebElement err : li){
			// System.out.println(err.getText());
			// }
			// //System.out.println(errorList.getText());
			// }
			System.out.println("erros where found!");
			return true;
		} catch (Exception e) {
			System.out.println("no erros");
			return false;
		}

	}

	private void fillNewUserDetails(String email, String emailConf,
			String firstName, String lastName, String password,
			String passwordConf) {
		this.email.sendKeys(email);
		this.emailconf.sendKeys(emailConf);
		this.firstName.sendKeys(firstName);
		this.lastName.sendKeys(lastName);
		this.password.sendKeys(password);
		this.passconf.sendKeys(passwordConf);
		submitBtn.submit();

	}

	public CardoCommunityHomePage signIn(String username, String password)
			throws Exception {
		WebElement signInForm = driver.findElement(By.id("logregLoginWrapper"));
		WebElement email = signInForm.findElement(By.id("email"));
		WebElement pass = signInForm.findElement(By.id("password"));
		WebElement signIn = signInForm.findElement(By.id("submit"));
		email.sendKeys(username);
		pass.sendKeys(password);
		signIn.click();
		try {
			WebElement closeConfirmMail = driver.findElement(By
					.id("TB_closeAjaxWindow"));
			closeConfirmMail.click();
		} catch (Exception e) {
			// do nothing
		}
		return new CardoCommunityHomePage(driver);
	}

	@Override
	public boolean isInModule() {
		return false;
	}

}
